﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02._IPQC.AddIPQCData.IPQCRemarkData_
{
    /// <summary>
    /// 添加检测标准备注表数据
    /// </summary>
    public class IPQCRemarkDataHandler : IRequestHandler<IPQCRemarkDataCommand, Result>
    {
        public IBaseRepository<IPQCRemarkData> IPQCRemarkDataRepository;
        public IMapper mapper;

        public IPQCRemarkDataHandler(IBaseRepository<IPQCRemarkData> iPQCRemarkDataRepository, IMapper mapper)
        {
            IPQCRemarkDataRepository = iPQCRemarkDataRepository;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(IPQCRemarkDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            int list = 0;
            int i = 0;
            foreach (var item in request.IPQCRemarkData_)
            {
                IPQCRemarkData iPQCRemarkData = new IPQCRemarkData()
                {
                    IPQCRemarkDataId = request.IPQCRemarkDataId,
                    IPQCId = request.IPQCId,
                    IPQCRemarkData_ = item.ToString(),
                    PRQSId = Convert.ToInt32(request.PRQSId[i]),
                    IsIPQCRemarkData = false,
                };
                list = IPQCRemarkDataRepository.Add(iPQCRemarkData);
                i++;
            }
            if (list > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
